// @ts-ignore
/* eslint-disable */

import { request, type RequestOptions } from '@/utils/request';
import { CustomerSource, EnterpriseType, CustomerCategory, Gender } from '@/enums';
import type { BatchShareRequest } from '@/types/customer';

// 客户搜索参数
export interface CustomerSearch {
  index: number;
  pageSize: number;
  companyName?: string;
  address?: string;
  contactTel?: string;
  contactName?: string;
  category?: CustomerCategory;
}

// 联系人搜索参数
export interface ContactSearch {
  customerId: string;
  index: number;
  pageSize: number;
  name?: string;
  phone?: string;
}

// 客户信息
export interface CustomerOut {
  id: string;
  companyName: string;
  companyPhone: string;
  companyEmail: string;
  website: string;
  source: CustomerSource;
  province: string;
  city: string;
  address: string;
  enterpriseType: EnterpriseType;
  category: CustomerCategory;
  background: string;
  remarks: string;
  miniProgramCode?: string;
  contacts: ContactOut[];
  creatTime: string;
  updateTime?: string;
  ownerId?: string;
  ownerName?: string;
  sharedUserIds?: string;
  sharedUsers?: SharedUserInfo[];
  isPublic: boolean;
}

// 共享用户信息
export interface SharedUserInfo {
  userId: string;
  userName: string;
  realName: string;
}

// 联系人信息
export interface ContactOut {
  id: string;
  customerId: string;
  name: string;
  email: string;
  backupEmail: string;
  phone: string;
  backupPhone?: string;
  position: string;
  province?: string;
  city?: string;
  address?: string;
  birthday?: string;
  gender: Gender;
  remarks: string;
  isPrimary: boolean;
  creatTime: string;
  updateTime?: string;
}

// 客户列表结果
export interface CustomerSearchOut {
  items: CustomerOut[];
  totalCount: number;
}

// 联系人列表结果
export interface ContactSearchOut {
  items: ContactOut[];
  totalCount: number;
}

/**
 * 获取所有客户列表
 */
export async function getCustomerList(params: CustomerSearch, options?: RequestOptions) {
  return request<CustomerSearchOut>('/customer/list', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}



/**
 * 获取可选客户列表
 */
export async function getSelectCustomerList(params: CustomerSearch, options?: RequestOptions) {
  return request<CustomerSearchOut>('/customer/selectlist', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}
/**
 * 获取我的客户列表
 */
export async function getMyCustomerList(params: CustomerSearch, options?: RequestOptions) {
  return request<CustomerSearchOut>('/customer/mylist', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}
/**
 * 获取公海客户列表
 */
export async function getPublicCustomerList(params: CustomerSearch, options?: RequestOptions) {
  debugger
  return request<CustomerSearchOut>('/customer/publiclist', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}
/**
 * 获取客户详情
 */
export async function getCustomerDetail(id: string, options?: RequestOptions) {
  return request<CustomerOut>(`/customer/detail?id=${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 创建客户
 */
export async function createCustomer(data: Omit<CustomerOut, 'id' | 'creatTime' | 'updateTime'>, options?: RequestOptions) {
  return request<void>('/customer/create', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 更新客户
 */
export async function updateCustomer(data: Partial<CustomerOut> & { id: string }, options?: RequestOptions) {
  return request<void>('/customer/update', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 删除客户
 */
export async function deleteCustomer(id: string, options?: RequestOptions) {
  return request<void>(`/customer/delete?id=${id}`, {
    method: 'POST',
    ...(options || {}),
  });
}

/**
 * 批量删除客户
 */
export async function batchDeleteCustomer(ids: string[], options?: RequestOptions) {
  return request<void>('/customer/batchDelete', {
    method: 'POST',
    data: ids,
    ...(options || {}),
  });
}

/**
 * 获取联系人分页列表
 */
export async function getContactList(params: ContactSearch, options?: RequestOptions) {
  return request<ContactSearchOut>('/customer/contact/plist', {
    method: 'POST',
    data: {
      ...params,
      page: params.index
    },
    ...(options || {}),
  });
}

/**
 * 获取联系人详情
 */
export async function getContactDetail(id: string, options?: RequestOptions) {
  return request<ContactOut>(`/customer/contact/detail?id=${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 创建联系人
 */
export async function createContact(data: Omit<ContactOut, 'id' | 'creatTime' | 'updateTime'>, options?: RequestOptions) {
  return request<void>('/customer/contact/create', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 更新联系人
 */
export async function updateContact(data: Partial<ContactOut> & { id: string }, options?: RequestOptions) {
  return request<void>('/customer/contact/update', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 删除联系人
 */
export async function deleteContact(id: string, options?: RequestOptions) {
  return request<void>(`/customer/contact/delete?id=${id}`, {
    method: 'POST',
    ...(options || {}),
  });
}

// 导入相关类型定义
export interface CustomerImportData {
  rowIndex: number;
  companyName: string;
  province?: string;
  city?: string;
  address?: string;
  contactName?: string;
  contactPhone?: string;
  backupPhone?: string;
  password?: string;
  miniProgramCode?: string;
  category?: string;
}

export interface ImportFailedItem {
  rowIndex: number;
  companyName?: string;
  errorMessage: string;
}

export interface BatchImportResult {
  total: number;
  success: number;
  failed: number;
  failedItems: ImportFailedItem[];
}

/**
 * 批量导入客户
 */
export async function batchImportCustomer(importData: CustomerImportData[], options?: RequestOptions) {
  return request<BatchImportResult>('/customer/batchImport', {
    method: 'POST',
    data: importData,
    timeout:500000,
    ...(options || {}),
  });
}

/**
 * 获取所有客户
 */
export async function all(key: string, options?: RequestOptions) {
  return request<CustomerOut[]>('/customer/all', {
    method: 'GET',
    params: { key },
    ...(options || {}),
  });
}

/**
 * 批量共享客户
 */
export async function batchShareCustomer(params: BatchShareRequest, options?: RequestOptions) {
  return request<void>('/customer/batchShare', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}

/**
 * 批量转为公海客户
 */
export async function batchToPublicCustomer(ids: string[], options?: RequestOptions) {
  return request<void>('/customer/batchToPublic', {
    method: 'POST',
    data: ids,
    ...(options || {}),
  });
}

// 产品搜索参数
export interface ProductSearch {
  customerId: string;
  index: number;
  pageSize: number;
  name?: string;
  model?: string;
}

// 产品信息
export interface ProductOut {
  id: string;
  customerId: string;
  name: string;
  model: string;
  quantity: number;
  price: number;
  total: number;
  purchaseDate: string;
  warranty?: string;
  remark?: string;
  creatTime: string;
  updateTime?: string;
}

// 产品列表结果
export interface ProductSearchOut {
  items: ProductOut[];
  totalCount: number;
}

/**
 * 获取产品分页列表
 */
export async function getProductList(params: ProductSearch, options?: RequestOptions) {
  return request<ProductSearchOut>('/customer/product/list', {
    method: 'POST',
    data: {
      ...params,
      page: params.index
    },
    ...(options || {}),
  });
}

/**
 * 获取产品详情
 */
export async function getProductDetail(id: string, options?: RequestOptions) {
  return request<ProductOut>(`/customer/product/detail?id=${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 创建产品
 */
export async function createProduct(data: Omit<ProductOut, 'id' | 'creatTime' | 'updateTime'>, options?: RequestOptions) {
  return request<void>('/customer/product/create', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 更新产品
 */
export async function updateProduct(data: Partial<ProductOut> & { id: string }, options?: RequestOptions) {
  return request<void>('/customer/product/update', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 删除产品
 */
export async function deleteProduct(id: string, options?: RequestOptions) {
  return request<void>(`/customer/product/delete?id=${id}`, {
    method: 'POST',
    ...(options || {}),
  });
}

// 产品资料搜索参数
export interface ProductDocumentSearch {
  index: number;
  pageSize: number;
  key?: string;
  productCode?: string;
  productId?: string;
  customerId?: string;
  status?: number;
  title?: string;
}

// 产品资料信息
export interface ProductDocumentOut {
  id?: string;
  customerId: string;
  customerName?: string;
  productId: string;
  productName?: string;
  productCode?: string;
  title: string;
  content?: string;
  warrantyStartTime?: string;
  warrantyEndTime?: string;
  warranty?: number;
  status: number;
  statusName?: string;
  customizationItems?: CustomerCustomizationOut[];
  creatTime?: string;
  updateTime?: string;
}

// 客户定制项
export interface CustomerCustomizationOut {
  id: string;
  content: string;
  description?: string;
  materials?: CustomizationMaterialOut[];
}

// 定制物料
export interface CustomizationMaterialOut {
  id: string;
  materialId: string;
  materialCode?: string;
  materialName?: string;
  specification?: string;
  unit?: string;
  quantity: number;
}

// 产品资料列表结果
export interface ProductDocumentSearchOut {
  items: ProductDocumentOut[];
  totalCount: number;
}

/**
 * 获取产品资料分页列表
 */
export async function getProductDocuments(params: ProductDocumentSearch, options?: RequestOptions) {
  return request<ProductDocumentSearchOut>('/productdocument/productdocuments', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}

/**
 * 获取产品资料详情
 */
export async function getProductDocument(id: string, options?: RequestOptions) {
  return request<ProductDocumentOut>(`/productdocument/${id}`, {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 创建或更新产品资料
 */
export async function saveProductDocument(data: ProductDocumentOut, options?: RequestOptions) {
  return request<void>('/productdocument/edit', {
    method: 'POST',
    data,
    ...(options || {}),
  });
}

/**
 * 删除产品资料
 */
export async function deleteProductDocument(id: string, options?: RequestOptions) {
  return request<void>(`/productdocument/${id}/logic-delete`, {
    method: 'POST',
    ...(options || {}),
  });
}

/**
 * 更新产品资料状态
 */
export async function updateProductDocumentStatus(id: string, status: number, options?: RequestOptions) {
  return request<void>(`/productdocument/${id}/update-status`, {
    method: 'POST',
    data: status,
    ...(options || {}),
  });
}

// 设置密码相关类型定义
export interface SetPasswordRequest {
  customerIds: string[];
  password: string;
}

/**
 * 批量设置客户密码
 */
export async function batchSetCustomerPassword(params: SetPasswordRequest, options?: RequestOptions) {
  return request<void>('/customer/batchSetPassword', {
    method: 'POST',
    data: params,
    ...(options || {}),
  });
}

/**
 * 设置单个客户密码
 */
export async function setCustomerPassword(customerId: string, password: string, options?: RequestOptions) {
  return request<void>('/customer/setPassword', {
    method: 'POST',
    data: { customerId, password },
    ...(options || {}),
  });
}

// 客户分布相关类型定义
export interface CustomerDistributionData {
  provinceName: string;
  customerCount: number;
}

/**
 * 获取客户省份分布统计
 */
export async function getCustomerDistribution(options?: RequestOptions) {
  return request<CustomerDistributionData[]>('/customer/distribution', {
    method: 'GET',
    ...(options || {}),
  });
}

/**
 * 获取指定省份的客户列表
 */
export async function getProvinceCustomers(provinceName: string, options?: RequestOptions) {
  return request<CustomerOut[]>('/customer/province', {
    method: 'GET',
    params: { provinceName },
    ...(options || {}),
  });
}

/**
 * 添加客户共享用户
 */
export async function addSharedUsers(customerId: string, userIds: string[], options?: RequestOptions) {
  return request<void>(`/customer/${customerId}/shared-users/add`, {
    method: 'POST',
    data: userIds,
    ...(options || {}),
  });
}

/**
 * 移除客户共享用户
 */
export async function removeSharedUsers(customerId: string, userIds: string[], options?: RequestOptions) {
  return request<void>(`/customer/${customerId}/shared-users/remove`, {
    method: 'POST',
    data: userIds,
    ...(options || {}),
  });
}

/**
 * 设置客户共享用户（覆盖现有）
 */
export async function setSharedUsers(customerId: string, userIds: string[], options?: RequestOptions) {
  return request<void>(`/customer/${customerId}/shared-users/set`, {
    method: 'POST',
    data: userIds,
    ...(options || {}),
  });
}

/**
 * 设置客户责任人
 */
export async function setCustomerOwner(customerId: string, ownerId: string | null, options?: RequestOptions) {
  return request<void>(`/customer/${customerId}/owner`, {
    method: 'POST',
    data: { ownerId },
    ...(options || {}),
  });
}
